<style>
    .ibox label {
        margin-left: 8px;
        margin-bottom: 8px;
    }
</style>
<!-- BEGIN PAGE HEADER--><?php

$this->load->view('partials/breadcrumb.tpl', Array('_NO_IOSS_NEW' => '1')) ?>
<!-- END PAGE HEADER-->
<!-- BEGIN PAGE CONTENT-->
<script language="javascript">
    function defConfirm(url) {
        if (!confirm("确定要删除吗？")) {
            return false;
        }
        window.location.href = url;
        return true;
    }

    $(document).ready(function () {
        $("#reset").click(function () {
            $("select").val('');
            $("input[type='text']").val('');
        });

    });

    function op_check() {
        var selObj = $("#user_op")[0];
        if (selObj.selectedIndex < 1) {
            alert("请选择操作！");
            return false;
        }
        var allUnChecked = true;
        var checkBoxList = $("input[name^='id_time']");
        for (var i = 0; i < checkBoxList.length; i++) {
            if (checkBoxList[i].checked) {
                allUnChecked = false;
                break;
            }
        }

        if (allUnChecked) {
            alert("请选择要操作的用户！");
            return false;
        }
        if (!confirm("确定要 " + $("#user_op").find("option:selected").html() + " 选中的用户么？")) {
            return false;
        }
        $("#user_mng_frm").submit();
    }

    $(document).ready(function () {
        $("#checkall").on('click', function () {
            var ck = $(this).is(":checked");
            var checkBoxList = $("input[name^='id_time']");
            for (var i = 0; i < checkBoxList.length; i++) {
                checkBoxList[i].checked = ck;
                $(checkBoxList[i]).uniform();
            }
        });
    });
</script>
<div class="row">
    <div class="col-xs-12">
        <!-- Begin: life time stats -->
        <div class="portlet light">
            <div class="portlet-title portlet-title-noborder">
                <?= form_open('', Array('class' => 'form-inline', 'role' => 'form')) ?>
                <?php if ($this->_user->role_id != 3 && $this->_user->role_id != 5): ?>
                    <?= search_form_dropdown('submiter_id', array('' => '选择打款申请人...') + $allReviewsAdmin, filterValue('submiter_id'), '申请人', 'class="form-control input-xs"'); ?>
                <?php endif; ?>
                <?php if ($this->_user->role_id != 7): ?>
                    <?= search_form_dropdown('finance_id', array('' => '选择处理该申请的财务...') + $allFinance, filterValue('finance_id'), '处理该申请的财务', 'class="form-control input-xs"'); ?>
                <?php endif; ?>
                <?= search_form_dropdown('status', array('' => '选择记录状态...') + MPayRequest::getRStatus(), filterValue('status'), '记录状态', 'class="form-control input-xs"') ?>
                <div style="clear:both;height:10px;"></div>
                <?= search_form_input('payrequest_number_perpage', filterValue('payrequest_number_perpage'), '每页显示条数', 'class="form-control input-xs"'); ?>
                <div class="input-group input-large date-picker input-daterange" data-date=""
                     data-date-format="yyyy-mm-dd">
                    <input type="text" class="form-control" name="pay_request_create_time_begin" value="<?= filterValue('pay_request_create_time_begin') ?>" placeholder="申请开始时间" autoComplete="off">
                    <span class="input-group-addon">to </span>
                    <input type="text" class="form-control" name="pay_request_create_time_end" value="<?= filterValue('pay_request_create_time_end') ?>" placeholder="申请结束时间" autoComplete="off">
                </div>
                <div class="input-group input-large date-picker input-daterange" data-date=""
                     data-date-format="yyyy-mm-dd">
                    <input type="text" class="form-control" name="pay_request_update_time_begin" value="<?= filterValue('pay_request_update_time_begin') ?>" placeholder="处理开始时间" autoComplete="off">
                    <span class="input-group-addon">to </span>
                    <input type="text" class="form-control" name="pay_request_update_time_end" value="<?= filterValue('pay_request_update_time_end') ?>" placeholder="处理结束时间" autoComplete="off">
                </div>
                <button type="button" class="btn default" id="reset">重置条件</button>
                <button type="submit" class="btn green about_search">搜索 <i class="fa fa-search"></i></button>
                <button type="button" class="btn purple" onclick="iBox()">导出选项</button>
                <label style="float: right;margin-right: 20px" class="label label-danger">总计<?php echo $total_number ?>
                    条</label>
                <?= form_close() ?>
                <script>
                    function iBox() {
                        $('#choose-field').toggle(500, 'swing')
                    }
                </script>
            </div>

            <div style="display:none;" class="portlet-body" id="choose-field">
                <form action="<?= site_url('/common/ExcelExport/excel_export') ?>" method="post" id="export_form">
                    <div class="well well-sm" style="margin: 10px 0;">
                        <div class="checkbox" style="margin-bottom: 15px;">
                            <b><i>请选择要导出的字段：</i></b>
                            <label>
                                <input id="all" type="checkbox">
                                全选/全不选
                            </label>
                        </div>
                        <div class="checkbox ibox">
                            <label>
                                <input class="fbox" name="field[]" value="id" type="checkbox">id
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="operator_id" type="checkbox">接单人
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="admin_group_name" type="checkbox">接单人所属组
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="task_id" type="checkbox">所属任务
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="asin" type="checkbox">任务sain
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="product_image" type="checkbox">任务图片
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="comment_link" type="checkbox">评论连接
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="shop_id" type="checkbox">店铺
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="order_no" type="checkbox">订单编号
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="buyer_order_time" type="checkbox">买手下单时间
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="buyer_pay_time" type="checkbox">买手付款时间
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="true_product_price" type="checkbox">购买时商品价格
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="buyer_pay_total" type="checkbox">买手下单支付费用
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="buyer_pay_coin_type" type="checkbox">买家付款币种
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="payment_type" type="checkbox">打款类型
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="pay_method" type="checkbox">付款方式
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="contact" type="checkbox">收款联系方式
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="pay_account" type="checkbox">收款账户
                            </label>
                            <label>
                                <input class="fbox" checked name="field[]" value="bank" type="checkbox">开户行信息
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="amount" type="checkbox">应付金额
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="coin_type" type="checkbox">应付币种
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="rate_rmb" type="checkbox">对人民币汇率
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="add_image" type="checkbox">凭证
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="add_image_1" type="checkbox">凭证1
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="add_image_2" type="checkbox">凭证2
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="finance_id" type="checkbox">财务处理者
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="status" type="checkbox">状态
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="remark" type="checkbox">备注
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="create_time" type="checkbox">创建时间
                            </label>
                            <label>
                                <input class="fbox" name="field[]" value="update_time" type="checkbox">更新时间
                            </label>
                            <input type="hidden" name="request_ids" value="">
                            <input type="hidden" name="file_name" value="finance_apply">
                            <input type="hidden" name="excel_type" value="FinanceApply">
                        </div>
                        <div class="text-right">
                            <button id="export" type="button" class="text-right btn blue-madison">原版导出</button>
                            <button id="export-python" type="button" class="text-right btn red">快速导出</button>
                        </div>
                        <script>
                            $('#export').click(function () {
                                var ids = $("#app-ids").serializeArray();
                                if (!ids.length) {
                                    toastr.error('请选择要导出的申请记录！', '失败');
                                    return false;
                                }
                                var r_ids = [];
                                for (i in ids){
                                    r_ids.push(ids[i]['value'])
                                }
                                $("input[name='request_ids']").val(r_ids);

                                var data = $('#export_form').serialize();
                                if (data.indexOf('field') == -1) {
                                    toastr.error('请选择要导出的字段！', '失败');
                                    return false;
                                } else {
                                    var x = 1;
                                    $('#process').modal('show');
                                    setInterval(function () {
                                        y = 1;
                                        x += parseFloat(Math.random(0, 1));
                                        var width = x.toFixed(2) + '%';
                                        if (parseInt(x) >= 99) {
                                            x = y / (y + 1) * 100;
                                            y++;
                                        }
                                        $('#show-process').html(width);
                                        $('#show-process').css('width', width)
                                    }, 1000);
                                    $.ajax({
                                        url: '<?=site_url("/common/ExcelExport/excel_export")?>',
                                        data: $('#export_form').serialize(),
                                        method: 'POST',
                                        dataType: 'json',
                                        success: function (output) {
                                            console.log(output)
                                            clearInterval();
                                            $('#show-process').html('100%');
                                            $('#show-process').css('width', '100%');
                                            $('#process').modal('hide');
                                            toastr.success(output.msg, '成功');
                                            document.location.href = '/' + output.url;
                                        },
                                        fail: function (data) {
                                            toastr.error(data.msg, '失败')
                                        }
                                    })
                                }
                            });
                            $('#export-python').click(function () {
                                var ids = $("#app-ids").serializeArray();
                                if (!ids.length) {
                                    toastr.error('请选择要导出的申请记录！', '失败');
                                    return false;
                                }
                                var r_ids = [];
                                for (i in ids){
                                    r_ids.push(ids[i]['value'])
                                }
                                $("input[name='request_ids']").val(r_ids);

                                var data = $('#export_form').serialize();
                                if (data.indexOf('field') == -1) {
                                    toastr.error('请选择要导出的字段！', '失败');
                                    return false;
                                } else {
                                    var x = 1;
                                    $('#process').modal('show');
                                    setInterval(function () {
                                        y = 1;
                                        x += parseFloat(Math.random(0, 1));
                                        var width = x.toFixed(2) + '%';
                                        if (parseInt(x) >= 99) {
                                            x = y / (y + 1) * 100;
                                            y++;
                                        }
                                        $('#show-process').html(width);
                                        $('#show-process').css('width', width)
                                    }, 1000);
                                    $.ajax({
                                        url: '<?=site_url("/common/ExcelExport/python_export")?>',
                                        data: $('#export_form').serialize(),
                                        method: 'POST',
                                        dataType: 'json',
                                        success: function (output) {
                                            console.log(output)
                                            clearInterval();
                                            $('#show-process').html('100%');
                                            $('#show-process').css('width', '100%');
                                            $('#process').modal('hide');
                                            toastr.success(output.msg, '成功');
                                            document.location.href = '/' + output.url;
                                        },
                                        fail: function (data) {
                                            toastr.error(data.msg, '失败')
                                        }
                                    })
                                }
                            });
                            $('#all').click(function () {
                                if ($(this).prop("checked") == true) {
                                    $(".fbox").prop('checked', true);
                                    $(".fbox").parent('span').addClass('checked');
                                    return;
                                } else {
                                    $(".fbox").prop('checked', false);
                                    $(".fbox").parent('span').removeClass('checked');
                                    return;
                                }
                            });
                        </script>
                    </div>
                </form>
            </div>
            <div class="portlet-body">
                <div class="row">
                    <div class="col-xs-12">
                        <div class="table-scrollable">
                            <form action="" method="post" onsubmit="return false;" id="app-ids">
                                <table class="table table-hover table-bordered table-striped">
                                    <thead>
                                    <tr>
                                        <th width="1%">
                                            <label>
                                                <input type="checkbox" id="merge_all" ">
                                            </label>
                                        </th>
                                        <th>ID</th>
                                        <th>打款申请人</th>
                                        <th class="text-center">处理财务账户</th>
                                        <th>状态</th>
                                        <th>申请时间</th>
                                        <th>处理时间</th>
                                        <th>备注</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <?php foreach ($list as $v) { ?>
                                        <tr>
                                            <td>
                                                <input type="checkbox" class="mbox" name="apply_ids[]"
                                                       value="<?= $v->id ?>" title="ID:<?= $v->id ?>">
                                            </td>
                                            <td><?= $v->id ?></td>
                                            <td><?= $allReviewsAdmin[$v->submiter_id] ?></td>
                                            <?php if ($this->_user->role_id == 7 && $v->status != 1): ?>
                                                <td class="text-center">
                                                    <button data-id="<?= $v->id ?>" type="button"
                                                            class="btn blue change-button"><?= $allFinance[$v->finance_id] ?></button>
                                                </td>
                                            <?php else: ?>
                                                <td class="text-center">
                                                    <?= $allFinance[$v->finance_id] ?>
                                                </td>
                                            <?php endif; ?>
                                            <td><?= MPayRequest::getRStatus($v->status) ?></td>
                                            <td><?php echo date('Y-m-d H:i:s', $v->create_time) ?></td>
                                            <td><?php echo date('Y-m-d H:i:s', $v->update_time) ?></td>
                                            <td width="10%"><?= $v->remark ?></td>
                                            <td width="12%">
                                                <div class="btn-group btn-group-solid">
                                                    <a href="<?= site_url($thisModule . $thisController . '/detail/' . $v->id) ?>"
                                                       class="btn btn-sm blue">
                                                        <i class="fa fa-play"></i> 查看 </a>
                                                </div>
                                            </td>
                                        </tr>
                                    <?php } ?>
                                    </tbody>
                                </table>
                            </form>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-xs-12 text-right">
                        <?= $pagination ?>
                    </div>
                </div>
            </div>
        </div>
        <!-- End: life time stats -->
    </div>
</div>
<div class="modal fade bs-example-modal-lg" id="process" data-backdrop="static" style="margin-top: 20%" tabindex="-1"
     role="dialog" aria-labelledby="myLargeModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">

            <div class="progress progress-striped active" style="height: 20px;">
                <div class="progress-bar progress-bar-info" style="width:0;" role="progressbar" aria-valuenow="0"
                     aria-valuemax="100" aria-valuemin="0" id="show-process">0%
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" id="change-finance">
    <div class="modal-dialog modal-sm" role="document">
        <div class="modal-content">

            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">×</span></button>
                <h5 class="modal-title" id="mySmallModalLabel">修改处理财务</h5>
            </div>
            <div class="modal-body inner-finance">

            </div>
        </div>
    </div>
</div>
<script>
    $('.change-button').click(function () {
        $('#change-finance').modal('show');
        var id = this.dataset.id;
        $.ajax({
            type: "POST",
            url: '<?=site_url('finance/apply/ajaxChange')?>',
            data: {'pay_request_id': id},
            dataType: "json",
            success: function (res) {
                var str = '';
                $.each(res.data, function (i) {
                    str += '<div style="text-align:center;margin-top: 5px;margin-bottom:8px;"><button data-requestid="' + res.request_id + '" data-id="' + i + '" class="btn blue change_item">' + res.data[i] + '</button></div>'
                });
                $('.inner-finance').html(str);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                console.log(xhr);
            }
        });
    });

    $("#change-finance").on("click", '.change_item', function () {
        $('.change_item').attr('disabled', 'disabled');
        var data = this.dataset;
        $.ajax({
            type: "POST",
            url: '<?=site_url('finance/apply/ajaxEdit')?>',
            data: data,
            dataType: "json",
            success: function (res) {
                if (res.status) {
                    $('.change_item').removeAttr('disabled');
                    toastr.success(res.msg, '成功');
                }
                setTimeout(function () {
                    location.href = '';
                }, 1000)
            },
            error: function (xhr) {
                console.log(xhr);
            }
        });
    });
    $('#merge_all').click(function () {
        if ($(this).prop("checked") == true) {
            $(".mbox").prop('checked', true);
            $(".mbox").parent('span').addClass('checked');
            return;
        } else {
            $(".mbox").prop('checked', false);
            $(".mbox").parent('span').removeClass('checked');
            return;
        }
    });

</script>
